package com.sms.controller;


import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.sms.common.A;
import com.sms.pojo.Products;
import com.sms.service.impl.ProductsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

//通过商品ID搜索商品信息
@WebServlet("/ProductsSearchById")
public class ProductsSearchByIdController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int productID=Integer.parseInt(req.getParameter("productID"));

        ProductsServiceImpl service=new ProductsServiceImpl();
        Products products = service.getById(productID);
        service.getSqlSession().close();
        if (products!=null) {
            resp.getWriter().write(JSONUtil.toJsonStr(A.success("商品搜索成功", products), JSONConfig.create().setDateFormat("yyyy-MM-dd")));
        } else {
            resp.getWriter().write(JSONUtil.toJsonStr(A.fail("商品搜索失败")));
        }
    }
}